<?php
/**
 * 功能.用户Store
 * User: zhangyale
 * Date: 2023/10/20
 */

namespace App\Service\Core\User\User;

use App\Library\Service\Verify;
use App\Model\Core\User\UserModel;
use App\Service\Core\Company\CompanyService;
use App\Service\Core\CoreBaseService;
use App\Service\ParamsService;
use App\Traits\ModelStoreTrait;

/**
 * @method $this setUsername($val) 姓名
 * @method $this setMobile($val) 手机号
 * @method $this setCompanyId($val) 单位
 * @method $this setStatus($val) 状态
 * @method $this setSex($val) 性别
 * @method $this setBirthday($val) 出生日期
 * @method $this setRemark($val) remark
 * @method $this setCanaddPatientUserNum($val) 可添加就诊人数量
 * @package App\Service\Core\Sys\User\User
 */
class UserStoreService extends CoreBaseService
{
    use ModelStoreTrait;

    protected array $required_param = ['username', 'mobile', 'status'];

    /**
     * @param $user_id
     * @return array
     * @throws \Throwable
     */
    public function store($user_id = 0): array
    {
        $this->check_required_param($this->required_param);
        if (!empty($this->errors)) {
            return alert_info(1, $this->errors[0], $this->errors);
        }
        $remark = $this->getParam('remark');
        $username = $this->getParam('username');
        if (empty($username)) {
            return alert_info(1, '用户名不能为空');
        }
        $mobile = $this->getParam('mobile');
        if (!Verify::isMobile($mobile)) {
            return alert_info(1, '请输入正确的手机号');
        }
        $check = UserModel::getInstance()->getOneByMobile($mobile);
        if (!empty($check) && $user_id != $check['user_id']) {
            return alert_info(1, '手机号码重复');
        }
        $company_id = $this->getParam('company_id');
        if ((!is_numeric($company_id) || $company_id <= 0) && $remark == '导入用户') {
            return alert_info(1, '单位不能为空');
        }
        if (is_numeric($company_id) && $company_id > 0) {
            $check_company = CompanyService::getInstance()->checkUserUsable($company_id);
            if ($check_company['code'] != 0) {
                return $check_company;
            }
        }
        $sex = $this->getParam('sex', 0);
        if (!array_key_exists((int)$sex, ParamsService::$sex)) {
            return alert_info(1, '性别错误');
        }
        $status = $this->getParam('status');
        if (!array_key_exists((int)$status, ParamsService::$status)) {
            return alert_info(1, '状态错误');
        }
        $birthday = $this->getParam('birthday');
        if (!empty($birthday)) {
            $birthday = date('Y-m-d', strtotime($birthday));
        }
//        $canadd_patient_user_num = $this->getParam('canadd_patient_user_num');
//        if ((!is_numeric($canadd_patient_user_num) || $canadd_patient_user_num < 0) && !empty($canadd_patient_user_num)) {
//            return alert_info(1, '可添加就诊人数错误');
//        }
        $data = [
            'username' => $username,
            'mobile' => $mobile,
            'company_id' => $company_id,
            'sex' => $sex,
            'birthday' => $birthday,
            'status' => $status,
//            'canadd_patient_user_num' => is_numeric($canadd_patient_user_num) ? $canadd_patient_user_num : null,
            'remark' => $remark
        ];
        if ($user_id) {
            $user_info = UserModel::getInstance()->getOne($user_id);
            if (empty($user_info)) {
                return alert_info(1, '用户不存在');
            }
            $msg = '修改用户【' . $user_info['username'] . '】';
            $change_data = easy_check_change_data($user_info, $data);
            if (empty($change_data)) {
                return alert_info(1, '信息没有变化');
            }
//            if (easy_in_array(['username', 'mobile', 'sex', 'birthday'], array_keys($change_data))) {
//                //和缓基础信息是否改变
//                $data['hehuan_is_change'] = 1;
//            }
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $res = UserModel::getInstance()->updateOne($user_id, $data);
        } else {
            $msg = '添加用户【' . $username . '】';
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['created_by'] = $this->operate_by;
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $res = UserModel::getInstance()->addOne($data);
            $user_id = $res;
        }
        if ($res === false) {
            return alert_info(1, '系统繁忙，请稍后再试');
        }
        $this->addLog('用户管理', $msg, $this->operate_id, $this->operate_by);
        CompanyService::getInstance()->changeUserNum($company_id);
        return alert_info(0, '操作成功', ['user_id' => $user_id, 'username' => $username]);
    }
}
